
var util = require('../../utils/util.js');
var url = require('../../utils/url.js');
var common = require('../../utils/common.js');
var that;
var start = 1;
var pageNum = 10;
var totalPage;
var roomType = 0;

Page({
  /**
   * 页面的初始数据
   */
  data: {
    allRooms: [],
    currentTab: 0,
    isEmpty: false,
    listImg: [],
    list: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this;
  },

  onShow: function () {
    start = 1;
    that.getRoomListByType(start);
  },

  onPullDownRefresh: function () {
    start = 1;
    that.getRoomListByType(start);
  },

  onReachBottom: function () {
    start++;
    if (start > totalPage) {
      // wx.showToast({
      //   title: "已加载全部",
      //   icon: '',
      //   duration: 2000
      // })
    } else {
      that.getRoomListByType(start);
    }
  },

  /** 
   * 点击tab切换 
   */
  swichNav: function (e) {
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      this.setData({
        currentTab: e.target.dataset.current
      })
      roomType = e.target.dataset.current;
      start = 1;
      that.getRoomListByType(start);
    }
  },

  /**
   * 根据房间类型获取房间:0-所有房间
   */
  getRoomListByType: function (start) {
    common.showLoading();
    wx.request({
      url: url.ALL_ROOM,
      method: 'POST',
      data: { start: start, num: pageNum, type: roomType },
      success: function (res) {
        console.log(res);
        common.closeLoading();
        wx.stopPullDownRefresh();
        if (res.data.data.data.length == 0) {
          that.setData({
            isEmpty: true,
          });
        } else {
          that.setData({
            isEmpty: false,
          });
        }
        if (start == 1) {
          that.setData({
            list: res.data.data.data,
          });
        } else {
          that.setData({
            list: that.data.list.concat(res.data.data.data),
          });
        }
        totalPage = res.data.data.size;
        if (that.data.list == null) {
          var toast = '加载失败';
          wx.showToast({
            title: toast,
            icon: '',
            duration: 2000
          })
        } else {
          var arrs = [];
          that.data.list.map(function (item) {
            arrs.push(item.imgs.split(',')[0])
          });

          that.setData({
            allRooms: that.data.list,
            listImg: arrs
          });

        }
      }, fail: function () {
        wx.stopPullDownRefresh();
        common.closeLoading();
        common.showTip('加载失败，请重试');
      }
    })
  },

  onItemClick: function (e) {
    var index = e.currentTarget.dataset.index;
    var roomId = this.data.allRooms[index].id;
    wx.navigateTo({
      url: '../../pages/roomDetail/roomDetail?roomId=' + roomId,
    })
  },



})